package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.util.AbToastUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.MainDataBean;
import com.example.administrator.caigou51.bean.UserBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.AppManager;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.util.YanZhengUtil;
import com.example.administrator.caigou51.widget.dialog.SweetDialogManager;

import cn.jpush.android.api.JPushInterface;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Administrator on 2016/8/26/026.
 */
public class LoginActivity extends GBaseActivity {
    private TextView textViewPhoneLogin;
    private View imageViewLineLeft;
    private ImageView imageViewTriggleLeft;
    private TextView textViewPassWordLogin;
    private View imageViewLineRight;
    private ImageView imageViewTriggleRight,imageViewYanZhengMa;
    private LinearLayout ll1;
    private EditText editText_login_num;
    private ImageView imageViewIconError;
    private EditText editText_login_password;
    private ImageView imageViewIcnoEye;
    private ImageView imageViewSavePass;
    private TextView textViewForGetPass,textViewGetYanZhengMa;
    private TextView textViewLogin;
    private boolean isShowPassWord=false;
    private boolean isSavePassWord=false;

    private String type="1";//登录类型1快速登录，2密码登录
    private String jpushId="";
    private CountDownTimer countDownTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewFor720_1280(R.layout.activity_login);
        findViewsById();
        initData();
    }

    private void findViewsById()
    {
        textViewPhoneLogin = (TextView) findViewById(R.id.textViewPhoneLogin);textViewPhoneLogin.setOnClickListener(this);
        imageViewLineLeft = (View) findViewById(R.id.imageViewLineLeft);
        imageViewTriggleLeft = (ImageView) findViewById(R.id.imageViewTriggleLeft);
        textViewPassWordLogin = (TextView) findViewById(R.id.textViewPassWordLogin);textViewPassWordLogin.setOnClickListener(this);
        imageViewLineRight = (View) findViewById(R.id.imageViewLineRight);
        imageViewTriggleRight = (ImageView) findViewById(R.id.imageViewTriggleRight);
        ll1 = (LinearLayout) findViewById(R.id.ll1);
        editText_login_num = (EditText) findViewById(R.id.editText_login_num);
        imageViewIconError = (ImageView) findViewById(R.id.imageViewIconError);imageViewIconError.setOnClickListener(this);
        editText_login_password = (EditText) findViewById(R.id.editText_login_password);
        imageViewIcnoEye = (ImageView) findViewById(R.id.imageViewIcnoEye);imageViewIcnoEye.setOnClickListener(this);
        imageViewSavePass = (ImageView) findViewById(R.id.imageViewSavePass);imageViewSavePass.setOnClickListener(this);
        textViewForGetPass = (TextView) findViewById(R.id.textViewForGetPass);textViewForGetPass.setOnClickListener(this);
        imageViewYanZhengMa= (ImageView) findViewById(R.id.imageViewYanZhengMa);
        textViewLogin = (TextView) findViewById(R.id.textViewLogin);textViewLogin.setOnClickListener(this);
        textViewGetYanZhengMa= (TextView) findViewById(R.id.textViewGetYanZhengMa);textViewGetYanZhengMa.setOnClickListener(this);
    }
    private void initData()
    {
        imageViewLineRight.setVisibility(View.INVISIBLE);
        imageViewTriggleRight.setVisibility(View.INVISIBLE);
        textViewGetYanZhengMa.setVisibility(View.VISIBLE);
        textViewPhoneLogin.setTextColor(getResources().getColor(R.color.color_1ca146));
        jpushId= JPushInterface.getRegistrationID(this);
        setTopBarBackGround(getResources().getColor(R.color.white));
        setLeftImg(View.VISIBLE,R.drawable.icon_back_green);
        setTopTitle("登录");
        getTopTitle().setTextColor(getResources().getColor((R.color.color_4b4f52)));
        setRightText(View.VISIBLE,"注册");
        getRightTextView().setTextColor(getResources().getColor((R.color.color_1ca146)));
        imageViewYanZhengMa.setImageResource(R.drawable.icon_yanzhengma);
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textViewLogin:
                //登录类型1快速登录，2密码登录
                if (!TextUtils.isEmpty(editText_login_num.getText()) && !TextUtils.isEmpty(editText_login_password.getText())) {
                    if (type.equals("2")) {
                        TaskLogin(false, LoginActivity.this,editText_login_num.getText().toString(), editText_login_password.getText().toString(), "", type, jpushId);
                    } else {
                        TaskLogin(false, LoginActivity.this, editText_login_num.getText().toString(), "", editText_login_password.getText().toString(), type, jpushId);
                    }
                }else
                {
                    AbToastUtil.showToast(this,"格式错误，请重新输入");
                }
                break;
            case R.id.imageViewIconError:
                editText_login_num.setText("");
                break;
            case R.id.imageViewIcnoEye:
                if (isShowPassWord)
                {
                    isShowPassWord=false;
                    imageViewIcnoEye.setImageResource(R.drawable.icon_eye_grey);
                    editText_login_password.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else
                {
                    isShowPassWord=true;
                    imageViewIcnoEye.setImageResource(R.drawable.icon_eye_black);
                    editText_login_password.setInputType(InputType.TYPE_NULL);
                }
                break;
            case R.id.imageViewSavePass:
                if (isSavePassWord)
                {
                    isSavePassWord=false;
                    imageViewSavePass.setImageResource(R.drawable.icon_circle_grey);

                }else
                {
                    isSavePassWord=true;
                    imageViewSavePass.setImageResource(R.drawable.icon_circle_green);
                }
                break;
            case R.id.textViewPhoneLogin:
                editText_login_num.setHint("请输入手机号码");
                editText_login_num.setInputType(InputType.TYPE_CLASS_PHONE);
                editText_login_num.setText("");
                imageViewLineLeft.setVisibility(View.VISIBLE);
                imageViewTriggleLeft.setVisibility(View.VISIBLE);
                imageViewLineRight.setVisibility(View.INVISIBLE);
                imageViewTriggleRight.setVisibility(View.INVISIBLE);
                textViewPhoneLogin.setTextColor(getResources().getColor(R.color.color_1ca146));
                textViewPassWordLogin.setTextColor(getResources().getColor(R.color.color_999999));
                imageViewYanZhengMa.setImageResource(R.drawable.icon_yanzhengma);
                textViewGetYanZhengMa.setVisibility(View.VISIBLE);
                editText_login_password.setHint("请输入验证码");
                type="1";

                break;
            case R.id.textViewForGetPass:
                Intent intent=new Intent(LoginActivity.this,FindPassActivity.class);
                startActivity(intent);
                break;
            case R.id.textViewPassWordLogin:
                editText_login_num.setHint("请输入用户名");
                editText_login_num.setInputType(InputType.TYPE_CLASS_TEXT);
                editText_login_num.setText("");
                imageViewLineLeft.setVisibility(View.INVISIBLE);
                imageViewTriggleLeft.setVisibility(View.INVISIBLE);
                imageViewLineRight.setVisibility(View.VISIBLE);
                imageViewTriggleRight.setVisibility(View.VISIBLE);
                textViewPassWordLogin.setTextColor(getResources().getColor(R.color.color_1ca146));
                textViewPhoneLogin.setTextColor(getResources().getColor(R.color.color_999999));
                imageViewYanZhengMa.setImageResource(R.drawable.icon_password);
                textViewGetYanZhengMa.setVisibility(View.GONE);
                editText_login_password.setHint("请输入密码");
                type="2";
                break;

            case R.id.textViewGetYanZhengMa:
                if (YanZhengUtil.isMobileNO(editText_login_num.getText().toString())) {
                    RegisterActivity.TaskGetYanZhengMa(false, LoginActivity.this, editText_login_num.getText().toString(), "3");
                    countDownTimer = new CountDownTimer(60 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            textViewGetYanZhengMa.setText((int) (millisUntilFinished / 1000) + "秒后可重发");
                            textViewGetYanZhengMa.setAlpha(0.5f);
                            textViewGetYanZhengMa.setEnabled(false);

                        }

                        @Override
                        public void onFinish() {
                            textViewGetYanZhengMa.setText("获取验证码");
                            textViewGetYanZhengMa.setEnabled(true);
                            textViewGetYanZhengMa.setAlpha(1f);
                        }
                    }.start();
                }else
                {
                    ToastUtil.showToast("请正确输入手机号码");
                }
                break;
        }
    }

    public static void TaskLogin(final boolean isloadmore, final GBaseActivity gBaseActivity, String username, String pass, String captchaPass, String type, String JpushId) {
        RequestServerManager.getInstanceDIY().handleMethodDIY(gBaseActivity, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_Login, Constant.Action.Action_Login, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_Login,username, pass,captchaPass,type,JpushId), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        UserBean userBean = JSON.parseObject(result.getData().toString(), UserBean.class);
                        App.saveUserBean(userBean);

                        if (App.getUserBean().getIs_verified()>0) {
                            SweetDialogManager.getInstance().showDialogBySuccessTitleTextTwoButton(gBaseActivity, "登录成功", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    AppManager.getAppManager().finishAllActivity();
                                    Intent intent2 = new Intent(gBaseActivity, MainActivity.class);
                                    gBaseActivity.startActivity(intent2);

                                    Intent intent = new Intent(gBaseActivity, StoreManagerActivity.class);
                                    gBaseActivity.startActivity(intent);
                                }
                            }, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    AppManager.getAppManager().finishAllActivity();
                                    Intent intent = new Intent(gBaseActivity, MainActivity.class);
                                    gBaseActivity.startActivity(intent);
                                }
                            }, "去逛逛", "进入店铺管理");
                        }else
                        {
                            SweetDialogManager.getInstance().showDialogBySuccessTitleTextTwoButton(gBaseActivity, "登录成功", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    AppManager.getAppManager().finishAllActivity();
                                    Intent intent2 = new Intent(gBaseActivity, MainActivity.class);
                                    gBaseActivity.startActivity(intent2);

                                    Intent intent = new Intent(gBaseActivity, ZhiZhiCheckActivity.class);
                                    gBaseActivity.startActivity(intent);
                                }
                            }, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    AppManager.getAppManager().finishAllActivity();
                                    Intent intent = new Intent(gBaseActivity, MainActivity.class);
                                    gBaseActivity.startActivity(intent);
                                }
                            }, "去逛逛", "进入资质认证");
                        }


                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }

    @Override
    public void doClickRightText() {
        super.doClickRightText();
        Intent intent=new Intent(this,RegisterActivity.class);
        startActivity(intent);
    }



}
